package com.xuecheng.learning.controller;

import com.xuecheng.api.learning.model.dto.UserDTO;
import com.xuecheng.api.teaching.model.dto.CompanyDTO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.domain.uaa.LoginUser;
import com.xuecheng.learning.common.utils.UAASecurityUtil;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.learning.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService  userService;

    /**
     * 远端调用查询UserDTO
     * @param userName
     * @return
     */
    @GetMapping("/l/user/{userName}")
    RestResponse<UserDTO> getUserByUserName(@PathVariable(value = "userName") String userName){

        RestResponse<UserDTO> response = userService.getUserByUserName(userName);

        return response;
    };


    @PostMapping("xc-member/list")
    public PageVO<UserDTO> getUserList(PageRequestParams params){
        CompanyDTO company = UAASecurityUtil.getCompany();

        PageVO<UserDTO> pageVO = userService.getUserListByCompanyId(params,company.getCompanyId());

        return pageVO;
    }
    @GetMapping("xc-member/get-by-phone")
    UserDTO getUserByPhone(@RequestParam String phone){
        Long companyId = UAASecurityUtil.getCompanyId();
        LoginUser user = UAASecurityUtil.getUser();
        UserDTO dto = userService.getUserByPhone(phone,companyId,user);
        return dto;
    }

    @PostMapping("xc-member/binding")
    public UserDTO bingUser(@RequestParam String phone,@RequestParam Long userId){
        LoginUser loginUser = UAASecurityUtil.getUser();
        CompanyDTO company = UAASecurityUtil.getCompany();
        UserDTO dto = userService.bingUser(phone,userId,loginUser,company);

        return dto;
    }
}
